package com.jianan.mis.app.basedata;

import static com.jianan.mis.util.ui.table.TableCreator.createSortedTableColumn;

import java.text.Collator;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

import com.jianan.mis.data.cache.EmployeeCache;
import com.jianan.mis.data.domain.Employee;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.util.StringUtils;
import com.jianan.mis.util.ui.table.AbstractInvertableTableSorter;

public class ChooseEmployeeDialog extends Dialog {

	private Text txtSearch;
	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			Employee employee = (Employee) element;
			String text = employeeTable.getColumn(columnIndex).getText();
			if (text.equals(EMPLOYEE_ID)) {
				return employee.getId().toString();
			} else if (text.equals(EMPLOYEE_NAME)) {
				return employee.getName();
			} else {
				return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}
	private TableViewer employeeTableViewer;

	private Table employeeTable;

	private Employee selectedEmployee;

	// Table column names/properties
	public static final String EMPLOYEE_ID = "员工编号";

	public static final String EMPLOYEE_NAME = "员工姓名";
	
	private String[] groups;
	
	private List<Employee> employees;
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public ChooseEmployeeDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setText("搜索员工:");

		txtSearch = new Text(container, SWT.BORDER);
		txtSearch.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				employeeTableViewer.setInput(null);
				if (StringUtils.isEmpty(txtSearch.getText())) {
					employeeTableViewer.setInput(employees);
					return;
				} else {
					List<Employee> matchedEmployees = new ArrayList<Employee>();
					for (Employee employee : employees) {
						if (StringUtils.contains(employee.getName(), txtSearch.getText().trim())) {
							matchedEmployees.add(employee);
						}
					}
					employeeTableViewer.setInput(matchedEmployees);
				}
			}
		});
		final GridData gd_txtSearch = new GridData(SWT.FILL, SWT.CENTER, true, false);
		txtSearch.setLayoutData(gd_txtSearch);

		employeeTableViewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
		employeeTableViewer.setLabelProvider(new TableLabelProvider());
		employeeTableViewer.setContentProvider(new ContentProvider());
		employeeTable = employeeTableViewer.getTable();
		employeeTable.setLinesVisible(true);
		employeeTable.setHeaderVisible(true);
		employeeTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		
		initializeTableViewer();
		employees = EmployeeCache.getInstance().getEmployeesByGroups(groups);
		employeeTableViewer.setInput(employees);
		
		//
		return container;
	}
	
	public void setEmployeeGroups(String[] groups) {
		this.groups = groups;
	}
	
	private void initializeTableViewer() {
		createSortedTableColumn(employeeTableViewer, SWT.CENTER, EMPLOYEE_ID, "", 120,
				new EmployeeIdSorter(), SWT.UP, false, null).chooseColumnForSorting();		
		createSortedTableColumn(employeeTableViewer, SWT.CENTER, EMPLOYEE_NAME, "", 120,
				new EmployeeNameSorter(), SWT.UP, false, null);
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	public Employee getSelectedEmployee() {
		return selectedEmployee;
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 270);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("选择员工");
	}
	
	// Convenience class which avoids casting in subclasses
	private abstract class AbstractSorter extends AbstractInvertableTableSorter {
		public int compare(Viewer viewer, Object o1, Object o2) {
			return compare((Employee) o1, (Employee) o2);
		}
		public abstract int compare(Employee e1, Employee e2);
	}
	private class EmployeeIdSorter extends AbstractSorter {
		public int compare(Employee e1, Employee e2) {
			return NumberUtils.compare(e1.getId(), e2.getId());
		}
	}
	private class EmployeeNameSorter extends AbstractSorter {
		public int compare(Employee e1, Employee e2) {
			return Collator.getInstance().compare(e1.getName(), e2.getName());
		}
	}
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			IStructuredSelection ss = (IStructuredSelection) employeeTableViewer
					.getSelection();
			selectedEmployee = (Employee) ss.getFirstElement();
			if (selectedEmployee == null) {
				MessageDialog.openInformation(getShell(), IMessageKeys.FAIL,
						"请先选择一位职员!");
				return;
			}
		}
		super.buttonPressed(buttonId);
	}

}
